﻿using FluentMigrator;
using System;

namespace NoteDbGenerator
{
    [Migration(1, "数据库初始化")]
    public class _001_DbInit : Migration
    {
        public override void Down()
        {
            Delete.Table("ArticleAttribute");
            Delete.Table("ArticleNote");
            Delete.Table("ArticleType");
            Delete.Table("Article");
        }

        public override void Up()
        {
            Create.Table("Article")
                .WithColumn("ObjectID").AsInt32().Identity().PrimaryKey()
                .WithColumn("CreateTime").AsDateTime()
                .WithColumn("LastUpdateTime").AsDateTime()
                .WithColumn("Description").AsCustom("NVARCHAR(512)")
                .WithColumn("FileUrl").AsCustom("NVARCHAR(512)");

            Create.Table("ArticleType")
                .WithColumn("ObjectID").AsInt32().Identity().PrimaryKey()
                .WithColumn("CreateTime").AsDateTime()
                .WithColumn("ChName").AsCustom("NVARCHAR(512)").Unique()
                .WithColumn("BizCode").AsCustom("NVARCHAR(512)");

            Create.Table("ArticleNote")
                .WithColumn("ObjectID").AsInt32().Identity().PrimaryKey()
                .WithColumn("ArticleID").AsInt32().ForeignKey("Article", "ObjectID").OnDelete(System.Data.Rule.Cascade)
                .WithColumn("CreateTime").AsDateTime()
                .WithColumn("LastUpdateTime").AsDateTime()
                .WithColumn("Description").AsCustom("NVARCHAR(2000)")
                .WithColumn("FileName").AsCustom("NVARCHAR(512)")
                .WithColumn("FormatFilePath").AsCustom("NVARCHAR(512)");

            Create.Table("ArticleAttribute")
                .WithColumn("ArticleID").AsInt32().PrimaryKey()
                .ForeignKey("Article", "ObjectID").OnDelete(System.Data.Rule.Cascade)
                .WithColumn("ArticleTypeID").AsInt32().PrimaryKey()
                .ForeignKey("ArticleType", "ObjectID").OnDelete(System.Data.Rule.Cascade);
        }
    }
}
